package com.lenovo.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TranslationDistinguish {
	public static void main(String[] args) {
		if (args.length != 1) {
            System.out.println("please enter the config file name");
            return;
        }

        File configFile = new File(args[0]);
        if (!configFile.exists() || !configFile.isFile()) {
            System.out.println("config file error");
            return;
        }
        
        try {
			Document doc = DocumentUtils.openDocument(configFile);
			String baseStr = doc.getElementsByTagName("baseDir").item(0).getTextContent().trim();
			String targetStr = doc.getElementsByTagName("targetDir").item(0).getTextContent().trim();
			String outStr = doc.getElementsByTagName("outDir").item(0).getTextContent().trim();
			File baseDir = new File(baseStr);
			File targetDir = new File(targetStr);
			File outDir = new File(outStr);
			File[] out = new File[]{new File(outDir, "base"), new File(outDir, "target"), new File(outDir, "common")};
			scanFiles(baseDir, targetDir, out);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void scanFiles(File base, File target,
			File[] out) {
		File[] files = base.listFiles();
		for (File file : files) {
			if(file.isDirectory()) {
				File[] subOut = new File[out.length];
				for (int i = 0; i < subOut.length; i++) {
					subOut[i] = new File(out[i], file.getName());
					subOut[i].mkdirs();
				}
				scanFiles(file, new File(target, file.getName()), subOut);
			} else {
				findDistinguish(file, new File(target, file.getName()), out);
			}
		}
	}

	private static void findDistinguish(File base, File target, File[] out) {
		System.out.println(base);
		try {
			Element element;
			
			List<Element> baseList = new ArrayList<Element>();
			List<Element> targetList = new ArrayList<Element>();
			List<Element> commonList = new ArrayList<Element>();
			
			Document baseDoc = DocumentUtils.openDocument(base);
			NodeList nodes = baseDoc.getElementsByTagName("string");
			for (int i = 0; i < nodes.getLength(); i++) {
				element = (Element) nodes.item(i);
				baseList.add(element);
			}
			nodes = baseDoc.getElementsByTagName("string-array");
			for (int i = 0; i < nodes.getLength(); i++) {
				element = (Element) nodes.item(i);
				baseList.add(element);
			}
			
			Document targetDoc = DocumentUtils.openDocument(target);
			nodes = targetDoc.getElementsByTagName("string");
			for (int i = 0; i < nodes.getLength(); i++) {
				element = (Element) nodes.item(i);
				targetList.add(element);
			}
			nodes = targetDoc.getElementsByTagName("string-array");
			for (int i = 0; i < nodes.getLength(); i++) {
				element = (Element) nodes.item(i);
				targetList.add(element);
			}
			
			Iterator<Element> baseIt = baseList.iterator();
			Iterator<Element> targetIt;
			Element element1;
			while (baseIt.hasNext()) {
				element = baseIt.next();
				targetIt = targetList.iterator();
				while (targetIt.hasNext()) {
					element1 = targetIt.next();
					if(element.getTagName().equals(element1.getTagName()) && element.getAttribute("name").equals(element1.getAttribute("name"))) {
						commonList.add(element);
						baseDoc.getElementsByTagName("resources").item(0).removeChild(element);
						targetDoc.getElementsByTagName("resources").item(0).removeChild(element1);
						break;
					}
				}
			}
			for (Element node1 : commonList) {
				for (Element node2 : baseList) {
					if(node1.getTagName().equals(node2.getTagName()) && node1.getAttribute("name").equals(node2.getAttribute("name"))) {
						baseList.remove(node2);
						break;
					}
				}
			}
			for (Element node1 : commonList) {
				for (Element node2 : targetList) {
					if(node1.getTagName().equals(node2.getTagName()) && node1.getAttribute("name").equals(node2.getAttribute("name"))) {
						targetList.remove(node2);
						break;
					}
				}
			}
			
			DocumentUtils.writeDocument(baseDoc, new File(out[0], base.getName()));
			DocumentUtils.writeDocument(targetDoc, new File(out[1], base.getName()));
			if(commonList.size() > 0) {
				Document common = createDocument(commonList);
				DocumentUtils.writeDocument(common, new File(out[2], base.getName()));
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
	
	private static Document createDocument(List<Element> list) throws ParserConfigurationException {
		Document document = DocumentUtils.createDocument();
		Element root = document.createElement("resources");
		document.appendChild(root);
		
		for (Element element : list) {
			root.appendChild(document.importNode(element, true));
		}
		return document;
	}
}
